home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / PARSING.SWG / 0008_Parsing A String.pas < prev   
Pascal/Delphi Source File  |  1994-08-24  |  3KB  |  115 lines

  1. {
  2. RN> I have a routine in one of my programs that that reads a delimited string
  3. RN> from a configuration file, the string is defined such as: ~040~055~099~144
  4. RN> etc. (these are message base area numbers)
  5.  
  6. RN> In the program a check is done to see if the current area exists or does
  7. RN> not exist in the list via a simple Pos() function.
  8.  
  9. RN> Works great! but.......
  10.  
  11. RN> I have been asked to include the capabilty to include a RANGE of numbers in
  12. RN> this list, this being due to the 255 char limit of a normal string.
  13.  
  14.  
  15. RN> So lets assume the list above will look like this:
  16.  
  17. RN> ~040~055~060-080~099~144
  18.  
  19. RN> How can I pull out the 060-080 and include all numbers between into the
  20. RN> list or actually, do a check, possibly creating a Set?
  21.  
  22. RN> OR would I have to create another function/configuration item to do this?
  23.  
  24. RN> I hope my explanation of what I wish to accomplish can be understood. <g>
  25.  
  26. RN> All replies are very welcomed!!
  27.  
  28. Try this, the code is ugly but it works!
  29. {Written, Tested and Compiled with BP 7.x}
  30.  
  31. uses crt;
  32.  
  33. type
  34.   Str3 = string[3];
  35. var
  36.   Area, RangeLo, RangeHi : str3;
  37.   List : String;
  38.  
  39. function Found(List:string;Area:str3):boolean;
  40. begin
  41.   if Pos(Area, List)>0 then begin
  42.     Found := true;
  43.   end else begin
  44.     {
  45.         Area not found yet, are there ranges??
  46.     }
  47.     if Pos('-', List)>0 then begin
  48.       {
  49.         Yes! Process ranges
  50.       }
  51.       while Pos('-', List) > 0 do begin
  52.         RangeLo := Copy(List, Pos('-', List)-3, 3);
  53.         {
  54.           Area must be BETWEEN Lo and hi otherwise it would have
  55.           been found by the first POS check. So if RangeLo is > Area
  56.           No need to lose time extracting RangeHi
  57.         }
  58.         if RangeLo<Area then begin
  59.           RangeHi := Copy(List, Pos('-', List)+1, 3);
  60.           if RangeHi > Area then begin
  61.             {
  62.                 Lo < Area < hi, We found a Match
  63.             }
  64.             Found := true;
  65.             {
  66.                 Kill list to exit while-loop
  67.             }
  68.             List := '';
  69.           end else begin
  70.             {
  71.                 Kill this range's DASH, POS only reports the first match
  72.             }
  73.             Delete(List, Pos('-', List), 1);
  74.           end;
  75.         end else begin
  76.             {
  77.                 Kill this range's DASH, POS only reports the first match
  78.             }
  79.           Delete(List, Pos('-', List), 1);
  80.         end;
  81.       end;
  82.       {
  83.         Only two possibilities when we get here
  84.             1- List = '' which means a match was found and list was
  85.                 cleared to exit the while-loop.
  86.             2- No match was found, in which case List is non-empty.
  87.       }
  88.       if List<>'' then
  89.         Found := false;
  90.     end else begin
  91.       Found := false;
  92.     end;
  93.   end;
  94. end;
  95.  
  96. var
  97.   X : byte;
  98.  
  99. begin
  100.   List := '~012~020~033~060-079~081~090~095-123~';
  101.   clrscr;
  102.   for X := 0 to 255 do begin
  103.     Area := chr(48 + (X div 100)) +
  104.             chr(48 + ((X mod 100) div 10)) +
  105.             chr(48 + ((X mod 10)));
  106.     writeln(Area, ' ', List, ' ', Found(List, Area));
  107.     if (not boolean(x mod 24)) and (x>0) then begin
  108.       while not keypressed do;
  109.       while keypressed do readkey;
  110.       clrscr;
  111.     end;
  112.   end;
  113. end.
  114.  
  115.